﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BookStore.Bussiness;
using BookStore.Data;

namespace BookStore.admin
{
    public partial class orderDetail : System.Web.UI.Page
    {
        static bool flag = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["Administrator"])))
            {
                Response.Redirect("~/admin/Login.aspx");
            }
            showAll();
            if (!Page.IsPostBack)
            {
                loadddl1();
                loadDDLProduct();
            }
        }
        public void showAll()
        {
            gvOrderdetail.DataSource = orderDetailService.getAll();
            gvOrderdetail.DataBind();
        }
        public void ResetControl() {
            txtquantity.Text ="";
            txtPrice.Text = "";
            rdbStatus.Checked = false;
        }
        public void loadddl1() {
            ddlOrder.DataSource = orderService.getAll();
            ddlOrder.DataTextField = "orderID";
            ddlOrder.DataValueField = "orderID";
            ddlOrder.DataBind();
        }
        public void loadDDLProduct() {
            ddlProduct.DataSource = productService.getAll();
            ddlProduct.DataTextField = "productName";
            ddlProduct.DataValueField = "productID";
            ddlProduct.DataBind();
        }
        public void insertOrder()
        {
            orderDetailInfo objOD = new orderDetailInfo();
            objOD.orderID = Convert.ToInt32(ddlOrder.SelectedValue);
            objOD.productID = Convert.ToInt32(ddlProduct.SelectedValue);
            objOD.quantity = Convert.ToInt32(txtquantity.Text);
            objOD.price = txtPrice.Text;
            objOD.status = rdbStatus.Checked;
            bool test = orderDetailService.insertOrderDetail(objOD);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('insert success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('insert failed');</script>");
            }
        }
        public void updateOrder()
        {
            orderDetailInfo objOD = new orderDetailInfo();
            objOD.orderID = Convert.ToInt32(ddlOrder.SelectedValue);
            objOD.productID =  Convert.ToInt32(ddlProduct.SelectedValue);
            objOD.quantity = Convert.ToInt32(txtquantity.Text);
            objOD.price = txtPrice.Text;
            objOD.status = rdbStatus.Checked;
            bool test = orderDetailService.updateOrderDetail(objOD);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('update success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('update failed');</script>");
            }
        }
        protected void lbtAddT_Click(object sender, EventArgs e)
        {
            flag = true;
            ResetControl();
            pnUpdate.Visible = true;
            pnView.Visible = false;
        }

        protected void lbtRefreshT_Click(object sender, EventArgs e)
        {
            showAll();
        }

        protected void lbtBackT_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;
        }

        protected void lbtUpdate_Click(object sender, EventArgs e)
        {
            if (flag==true)
            {
                insertOrder();
            }
            else
            {
                updateOrder();
            }
            showAll();
            pnUpdate.Visible = false;
            pnView.Visible = true;
        }

        protected void gvOrderdetail_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            flag = false;
            string orderID = gvOrderdetail.Rows[e.NewSelectedIndex].Cells[0].Text;
            orderDetailInfo objOD = orderDetailService.getbyID(orderID);
            ddlOrder.SelectedValue = objOD.orderID.ToString();
            ddlProduct.SelectedValue = objOD.productID.ToString();
            txtquantity.Text = objOD.quantity.ToString();
            txtPrice.Text = objOD.price.ToString();
            rdbStatus.Checked = objOD.status;
            pnUpdate.Visible = true;
            pnView.Visible = false;
        }

        protected void gvOrderdetail_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string orderID = gvOrderdetail.Rows[e.RowIndex].Cells[0].Text;
            bool test = orderDetailService.deleteOrderDetail(orderID);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('delete success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('delete failed');</script>");
            }
            showAll();
        }

        protected void gvOrderdetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvOrderdetail.PageIndex = e.NewPageIndex;
            showAll();
        }      
    }
}
